#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
#include <QMouseEvent>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    // 步骤1，安装过滤器 this当前窗口
    ui->label->installEventFilter(this);
}

Widget::~Widget()
{
    delete ui;
}

bool Widget::eventFilter(QObject *obj, QEvent *e)
{
    // 判断控件
    if (obj == ui->label)
    {
        if (e->type() == QEvent::MouseButtonPress)
        {
            QMouseEvent* ev = static_cast<QMouseEvent*>(e);
            QString str = QString("过滤器按下：x=%1, y=%2").arg(ev->x()).arg(ev->y());
            qDebug() << str.toUtf8().data();
            return true;
        }
    }

    // 其他交给父类
    return QWidget::eventFilter(obj, e);
}

